package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.NoteComment;
import cn.wolfcode.wolf2w.business.mapper.NoteCommentMapper;
import cn.wolfcode.wolf2w.business.query.NoteCommentQuery;
import cn.wolfcode.wolf2w.business.service.INoteCommentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 游记评论Service业务层处理
 *
 * @author 王南棕
 * @date 2025-09-15
 */
@Service
@Transactional
public class NoteCommentServiceImpl extends ServiceImpl<NoteCommentMapper,NoteComment> implements INoteCommentService {

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    @Override
    public IPage<NoteComment> queryPage(NoteCommentQuery qo) {
        //获取userID
        IPage<NoteComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        LambdaQueryWrapper<NoteComment> wrapper = new QueryWrapper<NoteComment>().lambda();
        wrapper.eq(NoteComment::getNoteId,qo.getNoteId());
        IPage<NoteComment> noteCommentIPage = baseMapper.selectPage(page, wrapper);
        //遍历noteCommentIPage的id作为ref_id,查询是否有子评论，添加到noteCommentIPage的refComment字段中
        for (NoteComment noteComment : noteCommentIPage.getRecords()) {
            //根据他的Userid 查询用户信息并放入user 字段中
            UserInfo userInfo = remoteUserInfoService.getOne(noteComment.getUserId(), SecurityConstants.INNER).getData();
            noteComment.setUser(userInfo);
            //是否存在ref Id
            if (noteComment.getRefId() != null) {
                //根据refId查询
                NoteComment refNoteComment = baseMapper.selectById(noteComment.getRefId());
                //根据他的Userid 查询用户信息并放入user 字段中
                UserInfo user = remoteUserInfoService.getOne(refNoteComment.getUserId(), SecurityConstants.INNER).getData();
                refNoteComment.setUser(user);
                noteComment.setRefComment(refNoteComment);
            }
        }
        return noteCommentIPage;
//        return lambdaQuery().eq(NoteComment::getNoteId,qo.getNoteId()).page( page);
    }

    @Override
    public void insert(NoteComment noteComment) {
        //获取用户id
        Long userId = SecurityContextHolder.getUserId();
        //创建时间
        Date date = new Date();
        noteComment.setUserId(userId);
        noteComment.setCreateTime(date);
        baseMapper.insert(noteComment);
    }
}
